package server.util;

import java.io.PrintWriter;

import player.Player;
import colour.ColourUtil;
import console.ANSIOutputBox;

/**
 *
 * @author Michael Hanns
 *
 */
public class PlayerConnectionLocal implements PlayerConnection {

    final private Player player;
    final private ANSIOutputBox out;

    public PlayerConnectionLocal(Player p, ANSIOutputBox out) {
        this.player = p;
        this.out = out;
    }

    @Override
    public boolean isPlayer(String name) {
        if (player.getAliveName().equalsIgnoreCase(name)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void broadcastMessage(String text) {
        out.addANSIText(text+"\n");
    }

    @Override
    public void broadcastMessageAt(int x, int y, int z, String text) {
        System.out.println(player.getName());
        if (player.x() == x && player.y() == y && player.z() == z) {
            //text = text.replace("The corpse of "
            //       + ColourUtil.colourise(player.getAliveName(), ColourUtil.player), "Your corpse");
            //text = text.replace(player.getAliveName(), "you");
            broadcastMessage(text);
        }
    }

    @Override
    public void broadcastTickNotification() {
        if (!player.suppressTick()) {
            broadcastMessage(ColourUtil.colourise("-->", ColourUtil.defaultColour) +
                    ColourUtil.colourise("TICK", ColourUtil.value) +
                    ColourUtil.colourise("<--", ColourUtil.defaultColour));
        }
    }

    @Override
    public boolean broadcastFromChannel(String channel, String text, String author) {
        if (player.subscribesTo(channel)) {
            if (isPlayer(author)) {
                broadcastMessage(ColourUtil.colourise("<" + channel + "> You say: '" + text + "'", ColourUtil.channel));
            } else {
                broadcastMessage(ColourUtil.colourise("<" + channel + "> " + author +
                        " says: '" + text + "'", ColourUtil.channel));
            }
            return true;
        }
        return false;
    }
}
